package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.qiantu.phone.R;
import com.qiantu.phone.glide.GlideApp;

import java.util.ArrayList;
import java.util.List;

/**
 * author： luzx
 * created on： 2020/10/19
 */

public class ImageAdapter extends RecyclerView.Adapter {

    private int maxCount;
    private List<String> data;
    private boolean editable;
    private int leftMargin;
    private LayoutInflater mLayoutInflater;
    private MyOnClickListener myOnClickListener;
    private OnItemClickListener mOnItemClickListener;
    private Context mContext;
    private RequestOptions options;

    public ImageAdapter(@Nullable Context context, @Nullable List<String> data, int maxCount) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.maxCount = maxCount;
        this.editable = true;
        this.data = data;
        myOnClickListener = new MyOnClickListener();
        leftMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_12);
        RoundedCorners roundedCorners = new RoundedCorners(mContext.getResources().getDimensionPixelSize(R.dimen.dp_4));
        options = RequestOptions.bitmapTransform(roundedCorners);
    }

    public ImageAdapter(@Nullable Context context, @Nullable List<String> data) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.editable = false;
        this.data = data;
        myOnClickListener = new MyOnClickListener();
        leftMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_12);
        RoundedCorners roundedCorners = new RoundedCorners(mContext.getResources().getDimensionPixelSize(R.dimen.dp_4));
        options = RequestOptions.bitmapTransform(roundedCorners).override(context.getResources().getDimensionPixelSize(R.dimen.dp_80));
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemHolder itemHolder = new ItemHolder(mLayoutInflater.inflate(R.layout.item_image, parent, false));
        itemHolder.btnDelete.setOnClickListener(myOnClickListener);
        itemHolder.image.setOnClickListener(myOnClickListener);
        return itemHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemHolder itemHolder = (ItemHolder) holder;
//        FrameLayout.LayoutParams fl = (FrameLayout.LayoutParams) itemHolder.image.getLayoutParams();
//        if(position == 0){
//            fl.leftMargin = 0;
//        }else{
//            fl.leftMargin = leftMargin;
//        }
        if (data != null && holder.getAdapterPosition() < data.size()){
            GlideApp.with(mContext)
                    .load(data.get(position))
                    .placeholder(itemHolder.image.getDrawable())
                    .apply(options)
                    .override(itemHolder.image.getLayoutParams().width)
                    .into(itemHolder.image);
            itemHolder.btnDelete.setVisibility(editable ? View.VISIBLE : View.GONE);
        }else{
            itemHolder.image.setImageResource(R.drawable.add_image_drawable);
            itemHolder.btnDelete.setVisibility(View.GONE);
        }
        itemHolder.image.setTag(position);
        itemHolder.btnDelete.setTag(position);
    }

    @Override
    public int getItemCount() {
        if(editable){
            //可编辑，未达到最大数添加上传按钮
            return data == null ? 1 : data.size() < maxCount ?  data.size() + 1 : data.size();
        }else{
            return data == null ? 0 : data.size();
        }
    }

    public int getMaxCount() {
        return maxCount;
    }

    public List<String> getData() {
        return data;
    }

    public void setData(List<String> data) {
        this.data = data;
        notifyDataSetChanged();
    }

//    public void addData(List<LocalMedia> items) {
//        if(this.data == null){
//            this.data = new ArrayList<>();
//        }
//        if(items != null){
//            for(LocalMedia localMedia : items){
//                data.add(localMedia.getRealPath());
//            }
//        }
//        notifyDataSetChanged();
//    }

    public void addData(String items) {
        if(this.data == null){
            this.data = new ArrayList<>();
        }
        data.add(items);
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(@Nullable OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(RecyclerView.Adapter adapter, View view, int position);
    }

    class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int position = 0;
            switch (v.getId()) {
                case R.id.btn_delete:
                    position = (int) v.getTag();
                    if(position < data.size()){
                        data.remove(position);
                        notifyDataSetChanged();
                    }
                    break;
                case R.id.image:
                    if(mOnItemClickListener != null){
                        position = (int) v.getTag();
                        mOnItemClickListener.onItemClick(ImageAdapter.this, v, position);
                    }
                    break;
            }
        }
    }

    class ItemHolder extends RecyclerView.ViewHolder {

        ImageView image;
        View btnDelete;

        ItemHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }
}
